/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bancodados;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author atavares
 */
public class ConexaoBD {
    Connection conexao;
    Statement instrucao;
    ResultSet resultado, chaves;
    private static ConexaoBD conexaoBD;

    private ConexaoBD() throws ClassNotFoundException, SQLException
    {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionUrl = "jdbc:mysql://localhost/identificadorperfil?" +
                               "user=root";
        conexao = (Connection) DriverManager.getConnection(connectionUrl);
        instrucao = conexao.createStatement();
    }

    public static ConexaoBD getConexao() throws ClassNotFoundException, SQLException
    {
        if (conexaoBD == null)
            conexaoBD = new ConexaoBD();
        return conexaoBD;
    }

    public ResultSet realizarInstrucao(String sql,boolean update) throws SQLException
    {
        if(conexao != null)
        {
            if(update)
            {
                instrucao.executeUpdate(sql);
                return null;
            }
            else
                return instrucao.executeQuery(sql);
        }
        return null;
    }
    public void rollback() throws SQLException
    {
        conexao.setAutoCommit(false);
        conexao.rollback();
        conexao.setAutoCommit(true);
    }
    public void commit() throws SQLException
    {
        conexao.setAutoCommit(false);
        conexao.commit();
        conexao.setAutoCommit(true);
    }
    public void begin() throws SQLException
    {
        realizarInstrucao("begin", true);
    }
}
